﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace WDTAsg2.DbBizObject
{
    public class UserInfo
    {
        private static List<User> users;

        /// <summary>
        /// Get user by their ID
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static User GetByID(string email)
        {
            return users.AsQueryable().First(customer => customer.Email == email);
        }

        /// <summary>
        /// Updates a user's profile information
        /// </summary>
        /// <param name="email"></param> The email of the user to edit
        /// <param name="firstname"></param>
        /// <param name="lastname"></param>
        /// <param name="age"></param>
        /// <param name="address"></param>
        public static void UpdateUserByEmail(string email, string firstname,
            string lastname, int age, string address)
        {
            // Grab the user info by their email
            CustomProfile profile = CustomProfile.GetProfile(email);
            // Update their properties and save
            profile.Personal.FirstName = firstname;
            profile.Personal.LastName = lastname;
            profile.Personal.Age = age;
            profile.Personal.Address = address;
            profile.Save();

        }

        /// <summary>
        /// Gets all the users from the DB
        /// </summary>
        /// <returns></returns>
        public static List<User> GetAllUsers()
        {
            string conString = ConfigurationManager.ConnectionStrings["HardwaresRUsCS"].ConnectionString;

            // Stored proc
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand("getAllUserInfo", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            //con.Close();

            // New aray
            users = new List<User>();

            // Call Read before accessing data.
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1], reader[2]));

                // display profile properties
                CustomProfile profile = CustomProfile.GetProfile(reader[0].ToString());

                // loop through and add a new user each time
                User user = new User
                {
                    FirstName = profile.Personal.FirstName,
                    LastName = profile.Personal.LastName,
                    Age = profile.Personal.Age,
                    Address = profile.Personal.Address,
                    Email = profile.UserName
                };

                // Dont return the super
                if (user.Email != "super")
                    users.Add(user);

            }

            // close
            reader.Close();
            return users;
        }

    }
}